Home:ALL Converter>how to use regex for populated values in mongodb

how to use regex for populated values in mongodb

Ask Time:2020-12-30T21:25:18         Author:chandrasekharreddy kotha

Json Formatter

I am working with mongodb regex for getting similar data in filter. But I don't know how to use regex for populated data in the same query.

Mongodb:

 assetAggregate = Assets.aggregate();
        assetAggregate.match({
                                company : new mongoose.Types.ObjectId(company),
                            })
                        .lookup({
                            from: "tickets",
                            localField: "_id",
                            foreignField: "assets",
                            as: "ticket_assets"
                        })
                        .lookup({
                            from:"locations",
                            localField:"location",
                            foreignField:"_id",
                            as:"locationData"
                        })
                        .unwind({ 'path': '$locationData', 'preserveNullAndEmptyArrays': true })
                        .lookup({
                            from:"user_dropdowns",
                            localField:"category",
                            foreignField:"_id",
                            as:"categoryData"
                        })
                        .unwind( { 'path': '$categoryData', 'preserveNullAndEmptyArrays': true })
                        .lookup({
                            from:"admin_dropdowns",
                            localField:"asset_status",
                            foreignField:"_id",
                            as:"assetStatusData"
                        })
                        .unwind( { 'path': '$assetStatusData', 'preserveNullAndEmptyArrays': true })
                        .lookup({
                            from:"user_dropdowns",
                            localField:"condition",
                            foreignField:"_id",
                            as:"assetCondition"
                        })
                        .unwind( { 'path': '$assetCondition', 'preserveNullAndEmptyArrays': true })
                        .lookup({
                            from:"stores",
                            localField:"deployedAt",
                            foreignField:"_id",
                            as:"deployedStore"
                        })
                        .unwind( { 'path': '$deployedStore', 'preserveNullAndEmptyArrays': true })
                        .lookup({
                            from:"company_employees",
                            localField:"assignedObj.allottedTo",
                            foreignField:"_id",
                            as:"assignedEmployee"
                        })
                        .unwind( { 'path': '$assignedEmployee', 'preserveNullAndEmptyArrays': true })
                        .lookup({
                            from:"locations",
                            localField:"using_location",
                            foreignField:"_id",
                            as:"assetUsingLocation"
                        })
                        .sort({"createdAt":-1})
                        .project({
                            "_id":1, "asset_name":1,"asset_code":1,
                            "status" : 1,"createdAt" : 1,"assetId": 1,
                            "location": {"$ifNull": ["$locationData", null]},
                            "category" : {"$ifNull": ["$categoryData", null]},
                            "asset_status" : {"$ifNull": ["$assetStatusData", null]},
                            "ticketCount": {"$size": "$ticket_assets"},
                            "deployedAt": {"$ifNull": ["$deployedStore", null]},
                            "assignedTo": {"$ifNull": ["$assignedEmployee", null]},
                            "condition": {"$ifNull": ["$assetCondition", null]},
                            "using_location": {"$ifNull": ["$assetUsingLocation", null]},
                            "predictive_maintenance": 1,
                            "maintenanceValidationData": 1,
                            "allocationValidationData": 1
                        })
                        .match({
                            "$or": [
                                { asset_name: { '$regex': payload.value, '$options': 'i' } },
                                { asset_code: { '$regex': payload.value, '$options': 'i' } },
                                { location:{name: { '$regex': payload.value, '$options': 'i' }} },
                            ]
                        });

In the above query I have been populating the location data like location:{ _id:xxxxxxxxx, name:"Guntur", .... } payload.value='Guntur'.

now I want to get the data based on the payload value. how can I use regex for that in match.

Author:chandrasekharreddy kotha,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/65507706/how-to-use-regex-for-populated-values-in-mongodb
yy